// 商品模型
const { Goods } = require('../../../models/good');
// 订单模型
const { Orders } = require('../../../models/order');
// 评论模型
const { Comment } = require('../../../models/comment');
// 用户模型
const { User } = require('../../../models/user');
// 统一数据返回类
const { DataReturn } = require('../../../common/returnclass');

module.exports = async (req, res) => {
	// 三级分类id
	let { _id } = req.query; 
	console.log(req.query);
	// 查询商品详细信息
	let goods = await Goods.findOne({_id});
	let comments = await Orders.find({order_goods:_id}).select('orders estimate order_belong -_id').populate({ 
			path: 'order_belong', 
			model: User,
			select: 'username avatar'
		}).populate({
			path: 'estimate',
			model: Comment
		});
	comments = comments.filter(item => {
		return item.estimate.length>0;
	})
	if (!goods) return DataReturn.getData(res, 200, goods, '暂无商品数据');
	// 响应
	DataReturn.getData(res, 200, {goods_detail:goods,comments:comments}, '查询商品详情成功')
}
